<template>
  <div class="gpu-node" :class="statusClass">
    <div class="gpu-icon">
      <div class="gpu-chip" :style="gpuTempStyle"></div>
      <div class="gpu-fan" :style="fanSpeedStyle"></div>
    </div>
    <div class="gpu-info">
      <div class="gpu-id">{{ displayId }}</div>
      <div class="gpu-utilization">
      <div class="util-bar" :style="utilizationStyle"></div>
      <span>{{ displayUtilization }}%</span>
    </div>
      <div class="gpu-temp">{{ gpu.temperature }}°C</div>
    </div>
    
    <!-- 添加的小下线按钮 -->
    <button 
      v-if="gpu.status === 'active'"
      class="deactivate-btn"
      @click.stop="deactivateGPU"
      title="Deactivate this GPU"
    >
      ×
    </button>
    <div v-if="showDetails" class="gpu-details">
      <div><strong>Status:</strong> {{ gpu.status === 'active' ? 'Active' : 'Inactive' }}</div>
      <div><strong>Last Active Step:</strong> {{ lastActiveStep }}</div>
      <div><strong>Utilization:</strong> {{ displayUtilization }}%</div>
      <div><strong>Temperature:</strong> {{ gpu.temperature }}°C</div>
      <div><strong>Server:</strong> {{ gpu.server }}</div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'

const props = defineProps({
  gpu: {
    type: Object,
    required: true
  },
  groupId: {
    type: String,
    required: true
  },
  loading: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['deactivate'])

const displayId = computed(() => {
  return props.gpu.id.split('-').slice(-2).join('-') // 显示最后两部分ID
})

const statusClass = computed(() => `status-${props.gpu.status}`)

const gpuTempStyle = computed(() => ({
  backgroundColor: getTemperatureColor(props.gpu.temperature)
}))

const fanSpeedStyle = computed(() => ({
  animationDuration: `${1.5 - (props.gpu.utilization / 100)}s`
}))

const displayUtilization = computed(() => {
  return props.gpu.status === 'offline' ? 0 : props.gpu.utilization
})

const utilizationStyle = computed(() => ({
  width: `${displayUtilization.value}%`,
  backgroundColor: props.gpu.status === 'offline' 
    ? '#d9d9d9' 
    : getUtilizationColor(displayUtilization.value)
}))

const lastActiveStep = computed(() => {
  if (props.gpu.status === 'offline' && props.group.fixedStep !== undefined) {
    return props.group.fixedStep
  }
  return store.step
})

function deactivateGPU() {
  emit('deactivate', props.gpu.id)
}

// Helper functions
function getUtilizationColor(util) {
  if (util > 80) return '#ff4d4f'
  if (util > 50) return '#faad14'
  return '#52c41a'
}

function getTemperatureColor(temp) {
  if (temp > 75) return '#ff4d4f'
  if (temp > 60) return '#fa8c16'
  return '#52c41a'
}
</script>

<style scoped>
.gpu-node {
  position: relative;
  padding: 10px;
  border-radius: 6px;
  background-color: #fafafa;
  transition: all 0.2s;
  min-width: 100px;
}

.status-active {
  border: 1px solid #d9d9d9;
}

.status-offline {
  border: 1px dashed #d9d9d9;
  opacity: 0.6;
}

.gpu-icon {
  position: relative;
  width: 50px;
  height: 30px;
  margin: 0 auto 8px;
  background-color: #333;
  border-radius: 4px;
  padding: 4px;
}

.gpu-chip {
  width: 30px;
  height: 20px;
  margin: 5px auto;
  border-radius: 2px;
  transition: background-color 0.3s;
}

.gpu-fan {
  position: absolute;
  right: 4px;
  top: 4px;
  width: 8px;
  height: 8px;
  background-color: #aaa;
  border-radius: 50%;
  animation: spin linear infinite;
}

.status-offline .gpu-fan {
  animation: none;
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.gpu-info {
  text-align: center;
  font-size: 0.8rem;
}

.gpu-id {
  font-weight: bold;
  margin-bottom: 4px;
}

.gpu-utilization {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-bottom: 4px;
}

.util-bar {
  height: 6px;
  border-radius: 3px;
  transition: all 0.3s;
}

.gpu-temp {
  font-size: 0.7rem;
  color: #666;
}

/* 新的下线按钮样式 */
.deactivate-btn {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 18px;
  height: 18px;
  padding: 0;
  background-color: #ff4d4f;
  color: white;
  border: none;
  border-radius: 50%;
  font-size: 12px;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.2s;
}

.gpu-node:hover .deactivate-btn {
  opacity: 1;
}

.deactivate-btn:hover {
  background-color: #ff7875;
  transform: scale(1.1);
}

.gpu-loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(255, 255, 255, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 2px;
}

.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid #1890ff;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* 调整deactivate按钮的z-index */
.deactivate-btn {
  z-index: 2;
}
</style>